package com.zbkj.crmeb.pdd;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Base64.Encoder;
import java.util.List;

import javax.imageio.ImageIO;

import com.pdd.pop.sdk.common.util.JsonUtil;
import com.pdd.pop.sdk.http.PopBaseHttpResponse;
import com.pdd.pop.sdk.http.PopClient;
import com.pdd.pop.sdk.http.PopHttpClient;
import com.pdd.pop.sdk.http.api.pop.request.PddGoodsAddRequest;
import com.pdd.pop.sdk.http.api.pop.request.PddGoodsAddRequest.GoodsPropertiesItem;
import com.pdd.pop.sdk.http.api.pop.request.PddGoodsAddRequest.SkuListItem;
import com.pdd.pop.sdk.http.api.pop.request.PddGoodsAuthorizationCatsRequest;
import com.pdd.pop.sdk.http.api.pop.request.PddGoodsCatRuleGetRequest;
import com.pdd.pop.sdk.http.api.pop.request.PddGoodsCatsGetRequest;
import com.pdd.pop.sdk.http.api.pop.request.PddGoodsImageUploadRequest;
import com.pdd.pop.sdk.http.api.pop.request.PddGoodsQuantityUpdateRequest;
import com.pdd.pop.sdk.http.api.pop.request.PddGoodsSpecGetRequest;
import com.pdd.pop.sdk.http.api.pop.request.PddGoodsSpecIdGetRequest;
import com.pdd.pop.sdk.http.api.pop.request.PddGoodsTemplatePropertyValueSearchRequest;
import com.pdd.pop.sdk.http.api.pop.response.PddGoodsAddResponse;
import com.pdd.pop.sdk.http.api.pop.response.PddGoodsAuthorizationCatsResponse;
import com.pdd.pop.sdk.http.api.pop.response.PddGoodsCatRuleGetResponse;
import com.pdd.pop.sdk.http.api.pop.response.PddGoodsCatsGetResponse;
import com.pdd.pop.sdk.http.api.pop.response.PddGoodsImageUploadResponse;
import com.pdd.pop.sdk.http.api.pop.response.PddGoodsQuantityUpdateResponse;
import com.pdd.pop.sdk.http.api.pop.response.PddGoodsSpecGetResponse;
import com.pdd.pop.sdk.http.api.pop.response.PddGoodsSpecIdGetResponse;
import com.pdd.pop.sdk.http.api.pop.response.PddGoodsTemplatePropertyValueSearchResponse;
import com.utils.ImageSearchUtils;
import com.utils.UploadUtil;

public class PddDemo {

	public static void main(String[] args) throws Exception {
		String clientId = "1c643a7e7a61478ba45b6d30072ac1f9";
		String clientSecret = "798e7f0f13a2dd573922e8d738ebe3c660bd7be6";
		String token = "8d67a595e1994963b106f541caf2a56557d91e24";
		PopClient client = new PopHttpClient(clientId, clientSecret);
		// 授权链接生成(返回code有效期十分钟)
//		String backUrl = URLEncoder.encode("http://www.zeroniu.com:8100/zero/callback/save", "UTF-8");
//		String authUrl = "https://fuwu.pinduoduo.com/service-market/auth?response_type=code&client_id=" + clientId + "&redirect_uri=" + backUrl + "&state=" + "自定义字段";
//		System.out.println(authUrl);
		
		// 获取token 365e4b620e8b43978185ec156802b19951314bfb
//		PopAccessTokenClient client = new PopAccessTokenClient(clientId, clientSecret);
//		AccessTokenResponse accessTokenResponse = client.generate("bde52af4ad294e478e4be008e60ac544ef336a07");
//		System.out.println(accessTokenResponse.getAccessToken());
		
		// 订单列表查询
//		PddOrderListGetRequest request = new PddOrderListGetRequest();
//		request.setRefundStatus(5);
//		request.setOrderStatus(5);
//		request.setStartConfirmAt(1668407272L);
//		request.setEndConfirmAt(1668493672L);
//		request.setPage(1);
//		request.setPageSize(10);
//	    PddOrderListGetResponse response = client.syncInvoke(request, "da7a40ac8f214bd69e6a8fff13a73d816577d13e");
//	    System.out.println(JSON.toJSONString(response));
		
		// 批量解密
//		DataListItem dataListItem = new DataListItem();
//		dataListItem.setDataTag("221115-600760687470526"); // 订单号orderSn
//		dataListItem.setEncryptedData("$a6QYjRAVn0bG$AgAAAAHB+GIGUB3gWQDfF7RhiW1gCzQmYUkJ4/2ZgWs=$0$$");
//		List<DataListItem> dataList = new ArrayList<>();
//		dataList.add(dataListItem);
//		PddOpenDecryptBatchRequest request = new PddOpenDecryptBatchRequest();
//		request.setDataList(dataList);
//	    PddOpenDecryptBatchResponse response = client.syncInvoke(request, "da7a40ac8f214bd69e6a8fff13a73d816577d13e");
//		System.out.println(JSON.toJSONString(response));
		
		// 密文解密成脱敏信息
//		DataListItem dataListItem = new DataListItem();
//		dataListItem.setDataTag("221115-600760687470526"); // 订单号orderSn
//		dataListItem.setEncryptedData("$a6QYjRAVn0bG$AgAAAAHB+GIGUB3gWQDfF7RhiW1gCzQmYUkJ4/2ZgWs=$0$$");
//		List<DataListItem> dataList = new ArrayList<>();
//		dataList.add(dataListItem);
//		PddOpenDecryptMaskBatchRequest request = new PddOpenDecryptMaskBatchRequest();
//		request.setDataList(dataList);
//	    PddOpenDecryptMaskBatchResponse response = client.syncInvoke(request, "da7a40ac8f214bd69e6a8fff13a73d816577d13e");
//		System.out.println(JSON.toJSONString(response));
		
		// 获取店铺信息
//		PddMallInfoGetRequest request = new PddMallInfoGetRequest();
//	    PddMallInfoGetResponse response = client4.syncInvoke(request, "d293c8afa2114a69963cf6de1cc6930c953abce4");
//		System.out.println(JSON.toJSONString(response));
		
		// 获取物流列表
//		PddLogisticsCompaniesGetRequest request = new PddLogisticsCompaniesGetRequest();
//        PddLogisticsCompaniesGetResponse response = client.syncInvoke(request);
//        System.out.println(JsonUtil.transferToJson(response));
		
		// 订单发货
//		PddLogisticsOnlineSendRequest request = new PddLogisticsOnlineSendRequest();
//        request.setFeature("备注信息: 哈哈哈");
//        request.setLogisticsId(115L);
//        request.setOrderSn("221115-600760687470526");
//        request.setRedeliveryType(1);
//        request.setTrackingNumber("78634040414465");
//        PddLogisticsOnlineSendResponse response = client.syncInvoke(request, token);
//        System.out.println(JsonUtil.transferToJson(response));
		
		// 类目获取
//		PddGoodsAuthorizationCatsRequest request = new PddGoodsAuthorizationCatsRequest();
//		request.setParentCatId(245L);
//	    PddGoodsAuthorizationCatsResponse response = client.syncInvoke(request, token);
//	    System.out.println(JsonUtil.transferToJson(response));
		
		// 标准类目获取
//		PddGoodsCatsGetRequest request = new PddGoodsCatsGetRequest();
//		request.setParentCatId(0L);
//	    PddGoodsCatsGetResponse response = client.syncInvoke(request, token);
//	    System.out.println(JsonUtil.transferToJson(response));
		
		// 获取类目发布规则
//		PddGoodsCatRuleGetRequest request = new PddGoodsCatRuleGetRequest();
//		request.setCatId(423L);
//	    PddGoodsCatRuleGetResponse response = client.syncInvoke(request, token);
//	    System.out.println(JsonUtil.transferToJson(response));
		
		
		// 运费模板获取
//		PddGoodsLogisticsTemplateGetRequest request = new PddGoodsLogisticsTemplateGetRequest();
//	    PddGoodsLogisticsTemplateGetResponse response = client.syncInvoke(request, token);
//	    System.out.println(JsonUtil.transferToJson(response));
		
		// 商品国家
//		PddGoodsCountryGetRequest request = new PddGoodsCountryGetRequest();
//	    PddGoodsCountryGetResponse response = client.syncInvoke(request, token);
//	    System.out.println(JsonUtil.transferToJson(response));
		
		// 获得类目下规格属性
//		PddGoodsSpecGetRequest request = new PddGoodsSpecGetRequest();
//		request.setCatId(423L);
//	    PddGoodsSpecGetResponse response = client.syncInvoke(request, token);
//	    System.out.println(JsonUtil.transferToJson(response));

		// 新增商家规格
//		PddGoodsSpecIdGetRequest request = new PddGoodsSpecIdGetRequest();
//		request.setParentSpecId(1216L);
//		request.setSpecName("XL");
//	    PddGoodsSpecIdGetResponse response = client.syncInvoke(request, token);
//	    System.out.println(JsonUtil.transferToJson(response));
		
		// 库存更新
//		PddGoodsQuantityUpdateRequest request = new PddGoodsQuantityUpdateRequest();
//		request.setGoodsId(437749807253L);
//		request.setQuantity(5L);
//		request.setOuterId("21-221429DJE000006-85B-44");
//	    PddGoodsQuantityUpdateResponse response = client.syncInvoke(request, token);
//	    System.out.println(JsonUtil.transferToJson(response));
		
		// 图片上传
		PddGoodsImageUploadRequest request = new PddGoodsImageUploadRequest();
		request.setImage("data:image/jpg;base64," + UploadUtil.getImgUrlToBase64ToSize("https://italytao.oss-cn-shanghai.aliyuncs.com/product-warehouse-image/1226022706/8e0fda61d5c5417b9a39d3a3592af786.JPG", 1500, 1200));
	    PddGoodsImageUploadResponse response = client.syncInvoke(request, token);
	    System.out.println(JsonUtil.transferToJson(response));
		
		// 商品新增
		/*PddGoodsAddRequest request = new PddGoodsAddRequest();
		List<String> galleryImgs = new ArrayList<>();
		galleryImgs.add("https://img.pddpic.com/open-gw/2022-11-25/b08fd10edf269ff3aab1b65e6be9e82c.jpg"); // 女孩
		request.setCarouselGallery(galleryImgs); // 商品轮播图
		request.setCatId(423L); // 类目id
		request.setCostTemplateId(301647687324739L); // 运费模板
		request.setCountryId(110); // 国家id
		List<String> galleryDetailImgs = new ArrayList<>(); // 注意需要控制大小
		galleryDetailImgs.add("https://img.pddpic.com/open-gw/2022-11-25/5a9d899283454ce4b464acf14076c944.jpg"); // 白衣女
		galleryDetailImgs.add("https://img.pddpic.com/open-gw/2022-11-25/55f83336d406563061aeda7ded3cef7b.jpg"); // 男士
		request.setDetailGallery(galleryDetailImgs); // 详情图
		request.setGoodsDesc("我是商品描述"); // 商品描述
		request.setGoodsName("我是标题test"); // 商品标题
		
		List<GoodsPropertiesItem> goodsProperties = new ArrayList<>();
		GoodsPropertiesItem goodsPropertiesItem = new GoodsPropertiesItem();
		goodsPropertiesItem.setRefPid(310L);
		goodsPropertiesItem.setVid(31952L);
		goodsPropertiesItem.setValue("Maxmara");
		goodsProperties.add(goodsPropertiesItem);
		request.setGoodsProperties(goodsProperties);
		
		request.setGoodsType(2); // 一般贸易
		request.setImageUrl("https://img.pddpic.com/open-gw/2022-11-25/5ff40eedbf34ae8517874565f9baf1d4.jpg"); // 大胡子
		request.setIsFolt(true); // 支持假一赔十
		request.setIsPreSale(false); // 是否预售
		request.setIsRefundable(true); // 7天无理由退换货
		request.setMarketPrice(99999L); // 参考价格，单位为分
		request.setOutGoodsId("883398456661"); // 商品goods外部编码
		request.setOutSourceGoodsId("5569871"); // 第三方商品Id
		request.setSecondHand(false); // 是否二手商品
		request.setShipmentLimitSecond(172800L); // 承诺发货时间（秒）
		request.setWarehouse("宁波保税仓"); // 保税仓，只在goods_type=3（直供商品）时入参, 需根据类目规则来
		request.setTwoPiecesDiscount(99); // 满两件打折比例
		// 商品sku信息
		SkuListItem skuListItem = new SkuListItem();
		skuListItem.setIsOnsale(1);
		skuListItem.setLimitQuantity(999L);
		skuListItem.setMultiPrice(69999L);
		skuListItem.setOutSkuSn("sku-code-0011");
		skuListItem.setOutSourceSkuId("1234561");
		// 单买属性
		skuListItem.setPrice(89999L);
		skuListItem.setQuantity(10000L);
		// sku属性
		//		List<PddGoodsAddRequest.SkuListItemSkuPropertiesItem> skuPropers = new ArrayList<>();
		//		PddGoodsAddRequest.SkuListItemSkuPropertiesItem skuItem = new PddGoodsAddRequest.SkuListItemSkuPropertiesItem();
		//		skuItem.setPunit("面料俗称");
		//		skuItem.setRefPid(349L);
		//		skuItem.setValue("麻");
		//		skuItem.setVid(10349L);
		//		skuPropers.add(skuItem);
		//		skuListItem.setSkuProperties(skuPropers);
		
		skuListItem.setSpecIdList("[2, 176991]");
		skuListItem.setThumbUrl("https://img.pddpic.com/open-gw/2022-11-25/7a5f897e9129e2c13c6b0cc9820ca42a.jpg"); // 鞋子
		skuListItem.setWeight(500L);
		// 新增sku属性
		List<SkuListItem> skuList = new ArrayList<>();
		skuList.add(skuListItem);
		request.setSkuList(skuList);
		// 发送请求
		PddGoodsAddResponse response = client.syncInvoke(request, token);
		System.out.println(JsonUtil.transferToJson(response));*/
		
	}
	

}
